<template>
  <div class="meetingDetail wboxnoborder" id="meetingDetail">
    <el-form ref="settingForm" :model="settingForm" label-width="200px" :rules="settingFormRoles" >
      <el-form-item label="初审用户：" prop="userId">
        <el-select v-model="settingForm.userId" placeholder="请选择">
          <el-option v-for="(item, index) in userlist" :key="index" :label="item.realname" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="savesetting" :loading="saveLoading">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'nesessarysetting',
  props: ['timestamp', 'unitIdList'],
  watch: {
    timestamp () {
      this.$refs['settingForm'].resetFields()
      // this.loadsetting()
      this.loaduserlist()
    }
  },
  data () {
    return {
      settingFormRoles: {
        userId: [{ required: true, message: '请选择初审用户', trigger: 'blur' }]
      },
      settingForm: {
        userId: ''
      },
      userlist: [],
      saveLoading: false,
      deptCode: null
    }
  },
  methods: {
    savesetting () {
      this.$refs['settingForm'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          let para = {
            trialUserId: this.settingForm.userId,
            trialUserName: this.userlist.find(item => { return item.id === this.settingForm.userId }).realname,
            unitIdList: this.unitIdList
          }
          this.$http
            .post(this.$http.defaults.youngAPI + `api/busTrialUser/saveSetting`, para)
            .then(response => {
              if (response.data.success) {
                this.$message({
                  message: '设置成功',
                  type: 'success'
                })
                this.$emit('closeDialog')
              } else {
                this.$message({
                  message: '设置失败:' + response.data.message,
                  type: 'error'
                })
              }
              this.saveLoading = false
            })
            .catch(error => {
              console.log(error)
              this.$message({
                message: '网络请求失败:' + error,
                type: 'error'
              })
              this.saveLoading = false
            })
        }
      })
    },
    loaduserlist () {
      this.$http
        .get(this.$http.defaults.youngAPI + `api/young/security/getBanshirenyuan?deptCode=` + this.deptCode)
        .then(response => {
          if (response.data.success) {
            this.userlist = response.data.result
          } else {
            this.$message({
              message: '读取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络读取失败:' + error,
            type: 'error'
          })
        })
    }
    // loadsetting () {
    //   this.$http
    //     .get(this.$http.defaults.youngAPI + `api/busUnit/getCurrentTrialUser`)
    //     .then(response => {
    //       if (response.data.success) {
    //         this.settingForm.userId = response.data.result
    //       } else {
    //         this.$message({
    //           message: '读取失败:' + response.data.message,
    //           type: 'error'
    //         })
    //       }
    //     })
    //     .catch(error => {
    //       console.log(error)
    //       this.$message({
    //         message: '网络读取失败:' + error,
    //         type: 'error'
    //       })
    //     })
    // }
  },
  mounted () {
    switch (localStorage.getItem('unitName')) {
      case '干部一处':
        this.deptCode = 1
        break
      case '干部二处':
        this.deptCode = 2
        break
      case '干部三处':
        this.deptCode = 3
        break
    }
    // this.loadsetting()
    this.loaduserlist()
  }
}
</script>
